package atessitura;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class KFrame extends JFrame implements MouseListener
{
	public static final Color[] backgroundColors={Color.black, Color.BLUE, Color.pink, Color.red, Color.green, Color.yellow, Color.white, Color.orange, Color.magenta};
	public static final String[] strings={"Hello Kevin!!", 
		"Ha! Clicking's not gonna work.", 
		"Still trying to get rid of me?!", 
		"Well that's not gonna work.", 
		"Maybe you should try clicking again!", 
		"Did it work?", "No?", 
		"How about now?", 
		"HAHAHAHAHAHAHAHA", 
		"Secret Message: You are super awesome <3",
		"You're so cute when you're being bombarded with flashing lights",
		"I do hope you're enjoying this!",
		"I like eggs."};
	private JPanel background;
	private int myInt;
	private Timer time;
	private int count;
	
	public KFrame()
	{
		this(0);
	}
	
	public KFrame(int i)
	{
		super(strings[i%strings.length]+" <3 you!");
		setVisible(true);
		count=i;
		myInt=count%strings.length;
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		int randomWidth=(int)(Math.random()*500);
		int randomHeight=(int)(Math.random()*500);
		int randomJWidth=(int)(Math.random()*200+800);
		int randomJHeight=(int)(Math.random()*300+200);
		setBounds(randomWidth, randomHeight, randomJWidth, randomJHeight);
		background=new JPanel();
		setContentPane(background);
		setBackground(backgroundColors[(int)(Math.random()*(backgroundColors.length-1))]);
		this.addMouseListener(this);
		time=new Timer(70, new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				changeBackgroundColor();
				
			}
			
		});
		time.start();
		playSound();
		repaint();
	}
	
	public void paint(Graphics g)
	{
		drawStringEverywhere(g);
	}
	
	public void playSound()
	{
		try {
			URL file = new File("src/406__tictacshutup__click-1-d.wav").toURI().toURL();
			//URL file = new File("sounds/72129__kizilsungur__sweetalertsound5.wav").toURI().toURL();		
			AudioInputStream audioIn = AudioSystem.getAudioInputStream(file);
			Clip clip = AudioSystem.getClip();
			clip.open(audioIn);
			clip.start();
			} catch (UnsupportedAudioFileException e) {
			e.printStackTrace();
			} catch (IOException e) {
			e.printStackTrace();
			} catch (LineUnavailableException e) {
			e.printStackTrace();
			}
	}
	
	public void changeBackgroundColor()
	{
		Color current=this.getBackground();
		Color changed=backgroundColors[(int)(Math.random()*(backgroundColors.length-1))];
		while(current.equals(changed))
		{
			changed=backgroundColors[(int)(Math.random()*(backgroundColors.length-1))];
		}
		this.setBackground(changed);
	}
	
	public void drawStringEverywhere(Graphics g)
	{
		g.setColor(Color.black);
		float size=50;
		g.setFont(g.getFont().deriveFont(size));
		g.drawString(strings[myInt], 100, (int)this.getSize().getHeight()/2);
		for (int i=0;i<25;i++)
		{
			size=(float)(Math.random()*50);
			int randomWidth=(int)(Math.random()*this.getSize().getWidth()-size);
			int randomHeight=(int)(Math.random()*this.getSize().getHeight());
			g.setColor(backgroundColors[(int)(Math.random()*(backgroundColors.length-1))+1]);
			g.setFont(g.getFont().deriveFont(size));
			g.drawString(strings[myInt], randomWidth, randomHeight);
		}
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		//myOpened.add(new KFrame());
		new KFrame((myInt+1)%strings.length);
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		new KFrame((myInt+1)%strings.length);
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
